<?php

	$action = fgsBase::getParam(0);
	$id = (int)fgsBase::getParam(1);
	$form = null;

	// ---------------------------------------------------------------
	// ADD or EDIT a game 
	// ---------------------------------------------------------------
	if ($action == 'add' || $action == 'edit') {
		$form = new htmlForm('game');

		if ($form->isCancel())
			fgsBase::redirect('admin_games');
		
		if ($action == 'add') {
			$form->addSection(tr('Add a game'));
		} else {
			$game = fgsGames::getById($id);
			if (!$game)
				fgsBase::redirect('admin_games');
			$form->addSection(sprintf(tr('Edit game #%d'), $id));

			$form->default_values['name'] = $game['name'];
			$form->default_values['gid'] = $game['gid'];
			$form->default_values['description'] = $game['description'];
			$form->default_values['url'] = $game['url'];
			$form->default_values['version_cmd'] = $game['version_cmd'];
			$form->default_values['launch_cmd'] = $game['launch_cmd'];
			$form->default_values['port'] = $game['port'];
			$form->default_values['is_activated'] = $game['is_disabled'] ? 0 : 1;
		}

		$form->addField(tr('Name'), 'name', 'text', true);
		$form->addField(tr('Gid'), 'gid', 'text', true);
		$form->addField(tr('Url'), 'url', 'text', true);
		$form->addField(tr('Port'), 'port', 'text', true);
		$form->addField(tr('Description'), 'description', 'textarea', true);
		$form->addField(tr('Activate'), 'is_activated', 'checkbox');
		$form->addField(tr('Image'), 'icon', 'file');
		$form->addSubmit(tr('Submit'));

		// add game if post is good
		if ($form->isPost() && $form->isValid()) {
			$name = $form->get('name');
			$gid = $form->get('gid');
			$url = $form->get('url');
			$port = (int)$form->get('port');
			$description = $form->get('description');
			$is_disabled = $form->get('is_activated') ? false : true;
			
			if ($action == 'add')
				$id = fgsGames::add($name, $description, $url, '', '', $gid, $port, $is_disabled);
			else 
				fgsGames::update($id, $name, $description, $url, '', '', $gid, $port, $is_disabled);

			$icon = $form->getFile('icon');
			if ($icon !== false)
				fgsGames::updateLogo($id, $icon['tmp_name']);

			fgsBase::redirect('admin_games');
		}


	// ---------------------------------------------------------------
	// SHOW a game
	// ---------------------------------------------------------------
	} else if ($action == 'show') {
		fgsGames::enable($id, 1);
		fgsBase::redirect('admin_games');
		
	// ---------------------------------------------------------------
	// HIDE a game
	// ---------------------------------------------------------------
	} else if ($action == 'hide') {
		fgsGames::enable($id, 0);
		fgsBase::redirect('admin_games');

	}
	
?>
